package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

@RestController
public class SearchController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;

    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws Exception {
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);

        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return this.searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return this.searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return this.searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return this.searchUser(qo);
           default:
               return  this.searchAll(qo);
        }

    }
    //全部
    private Object searchAll(SearchQueryObject qo) {
        Page<UserInfo> userPage = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfo.class, qo, "nickname","city","info");
        Page<Travel> tsPage = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                Travel.class, qo, "title","summary");
        Page<Strategy> stsPage = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                Strategy.class, qo, "title", "subTitle", "summary");
        Page<Destination> destPage = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME,
                Destination.class, qo, "title", "subTitle", "summary");
        SearchResultVO vo = new SearchResultVO();

        vo.setUsers(userPage.getContent());
        vo.setTravels(tsPage.getContent());
        vo.setStrategys(stsPage.getContent());
        vo.setDests(destPage.getContent());

        vo.setTotal(userPage.getTotalElements()+tsPage.getTotalElements()+stsPage.getTotalElements()+destPage.getTotalElements());


        HashMap<String, Object> map = new HashMap<>();
        map.put("result", vo);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //用户
    private Object searchUser(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfo.class, qo, "nickname","city","info");

        HashMap<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //游记
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                Travel.class, qo, "title","summary");

        HashMap<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //攻略
    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                Strategy.class, qo, "title", "subTitle", "summary");

        HashMap<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }


    //目的地
    private Object searchDest(SearchQueryObject qo) {
        //1.传入keyword如果是某个目的地,查询该目的地下所有的攻略
        Destination destination=destinationService.findByName(qo.getKeyword());

        SearchResultVO vo = new SearchResultVO();
        if (destination !=null){
            List<Strategy> sts = strategyService.findByDestId(destination.getId());
            List<Travel> st=travelService.findByDestId(destination.getId());
            List<UserInfo> us= userInfoService.findByCity(destination.getName());

            vo.setStrategys(sts);
            vo.setTravels(st);
            vo.setUsers(us);

            vo.setTotal(sts.size()+ st.size()+us.size()+0L);
        }
        //2.如果不是,数据返回0条
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", vo);
        map.put("dest", destination);
        map.put("qo", qo);
        return JsonResult.success(map);
    }
}
